﻿
using SqlSugar.Extensions;

namespace TestPressure
{
    public partial class AddDeviceForm : BaseForm
    {
        #region 属性

        private Device obj = new Device();

        #endregion

        #region 自定义方法

        /// <summary>重载
        /// 
        /// </summary>
        private void Reload()
        {
            DeviceNumberInput.Text = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
            DeviceNameInput.Text = "";
            DeviceAddressInputNumber.Text = "";
            PortAddressSelect.Text = "";
            BaudRateSelect.Text = "";
        }

        /// <summary>重复检测
        /// 
        /// </summary>
        /// <returns></returns>
        private bool CheckRepeat()
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["deviceName"] = DeviceNameInput.Text;
            List<Device> list = SqlSugarHelper.Inquireable<Device>(where);
            int count = list.Count;
            if (count > 0)
            {
                AntdUI.Message.error(this, where["deviceName"] + "设备已存在", Font);
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 系统方法
        public AddDeviceForm()
        {
            InitializeComponent();

        }

        private void AddDeviceForm_Load(object sender, EventArgs e)
        {
            this.Header.Text = "添加设备";
            Size size = new Size(int.Parse((330 * this.Dpi()).ToString()), int.Parse((270 * this.Dpi()).ToString()));
            this.MinimumSize = size;
            this.MaximumSize = size;
            Reload();
        }
        #endregion

        #region 点击事件
        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            if (DeviceNameInput.Text.Length == 0)
            {
                Error("请输入设备名称");
                DeviceNameInput.Focus();
                return;
            }
            obj.device_name = DeviceNameInput.Text;

            if (DeviceAddressInputNumber.Text.Length == 0)
            {
                Error("请输入设备地址");
                DeviceAddressInputNumber.Focus();
                return;
            }
            obj.device_address = DeviceAddressInputNumber.Text.ObjToInt();

            if (PortAddressSelect.Text.Length == 0)
            {
                Error("请选择串口地址");
                PortAddressSelect.Focus();
                return;
            }
            obj.port_address = PortAddressSelect.Text;

            if (BaudRateSelect.Text.Length == 0)
            {
                Error("请选择波特率");
                BaudRateSelect.Focus();
                return;
            }
            obj.baudrate = BaudRateSelect.Text.ObjToInt();
            obj.device_number = DeviceNumberInput.Text.ObjToInt();
            bool repeatRes = CheckRepeat();
            if (repeatRes)
            {
                return;
            }
            List<Device> list = new List<Device>();
            list.Add(obj);
            int res = SqlSugarHelper.Insertable(list);
            if (res > 0)
            {
                Reload();
                if (this.Owner is not null)
                {
                    DeviceManageForm frm = (DeviceManageForm)Owner;
                    frm.LoadList();
                }
                this.Close();
            }
            else
            {
                Alert.Error("添加设备失败", this);
            }
        }
        #endregion
    }
}
